package com.ymatou.iapi.sellerquery.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderCountArguments;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderCountBean;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderCountResponse;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSellerOrderCount;
import com.ymatou.query.seller.trading.api.parameter.SellerQuery.GetSellerOrderCountResult;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.common.entitie.SellerOrderStatusParams;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取商家订单数量 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerOrderCount {

	private static GetSellerOrderCountBean getsellerordercountbean;
	private static SellerOrderStatusParams sellerOrderStatusParams;
	// private static com.ymatou.query.seller.trading.api.service.GetSellerOrderCountCall getsellerordercountcall;
	private static com.ymatou.iapi.sellerquery.service.GetSellerOrderCountCall getsellerordercountcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家订单数量");
	}

	@Before
	public void caseUp() {
		getsellerordercountbean = new GetSellerOrderCountBean();
		sellerOrderStatusParams = new SellerOrderStatusParams();
		// getsellerordercountcall = new com.ymatou.query.seller.trading.api.service.GetSellerOrderCountCall();
		getsellerordercountcall = new com.ymatou.iapi.sellerquery.service.GetSellerOrderCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerordercountcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerOrderCountResponse expectedResponse = new GetSellerOrderCountResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSellerOrderCountResponse actualResponse = getsellerordercountcall
				.getSellerOrderCountResponse();
		Map<OrderStatusEnum, Integer> expect = expectedResponse
				.getAllRecordCountDic();
		Map<OrderStatusEnum, Integer> actual = actualResponse
				.getAllRecordCountDic();
		Iterator<OrderStatusEnum> keys = expect.keySet().iterator();
		while (keys.hasNext()) {
			OrderStatusEnum key = keys.next();
			boolean exists = actual.containsKey(key);
			DataUtil.verifyEquals(true, exists, "判断期望的key值:" + key + "是否存在");
			if (exists) {
				DataUtil.verifyEquals(expect.get(key), actual.get(key),
						"判断期望的Key:" + key + "的值是否正确");
			}
		}
	}

	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResponse(GetSellerOrderCountResponse response) {
		GetSellerOrderCountArguments args = new GetSellerOrderCountArguments();
		args.setBeginTime(getsellerordercountbean.getBeginTime());
		args.setEndTime(getsellerordercountbean.getEndTime());
		args.setIsShangouOrder(getsellerordercountbean.getIsShangouOrder());
		args.setSellerId(getsellerordercountbean.getSellerId());
		args.setOrderStatusList(getsellerordercountbean.getOrderStatusList());
		Map<OrderStatusEnum, Integer> dict = new HashMap<OrderStatusEnum, Integer>();
		List<GetSellerOrderCountResult> result = Execute(args);
		if (DataUtil.isNullOrEmpty(result) == false) {
			int total = 0;
			int n = 0;
			for (GetSellerOrderCountResult rs : result) {
				if (rs.getiTradingStatus() == OrderStatusEnum.SellerAccept
						.getValue()) {
					dict.put(rs.isbPaidInFull() ? OrderStatusEnum.SellerAccept
							: OrderStatusEnum.OrderPostPay, DataUtil.Integerr
							.GetValueOrDefault(rs.getOrderCount()));
				} else {
					if (dict.containsKey(OrderStatusEnum.forValue(rs
							.getiTradingStatus()))) {
						n = 0;
					}
					dict.put(OrderStatusEnum.forValue(rs.getiTradingStatus()),
							n + rs.getOrderCount());
				}
				total += rs.getOrderCount();
			}
			dict.put(
					OrderStatusEnum.forValue(OrderStatusEnum.Unkown.getValue()),
					total);// 所有
			if (!DataUtil.isNullOrEmpty(getsellerordercountbean
					.getOrderStatusList())) {
				for (int status : getsellerordercountbean.getOrderStatusList()) {
					if (!dict.containsKey(OrderStatusEnum.forValue(status))) {
						dict.put(OrderStatusEnum.forValue(status), 0);
					}
				}
			}
		}
		response.setAllRecordCountDic(dict);
	}

	@SuppressWarnings("rawtypes")
	private List<GetSellerOrderCountResult> Execute(
			GetSellerOrderCountArguments args) {
		Sp_GetSellerOrderCount sp = new Sp_GetSellerOrderCount();
		sp.setSellerId(args.getSellerId());
		sp.setIsShangouOrder(args.getIsShangouOrder());
		sp.setBeginTime(args.getBeginTime());
		sp.setEndTime(args.getEndTime());
		sp.setOrderStatus(args.getOrderStatusList());
		List<Map> result = sp.execute();
		return ConvertResult(result);
	}

	@SuppressWarnings("rawtypes")
	private List<GetSellerOrderCountResult> ConvertResult(List<Map> result) {
		List<GetSellerOrderCountResult> l = new ArrayList<GetSellerOrderCountResult>();
		if (!DataUtil.isNullOrEmpty(result)) {
			for (Map m : result) {
				GetSellerOrderCountResult o = new GetSellerOrderCountResult();
				o.setbPaidInFull(DataUtil.Booleann.GetValueOrDefault(m
						.get("bPaidInFull")));
				o.setiTradingStatus(DataUtil.Integerr.GetValueOrDefault(m
						.get("iTradingStatus")));
				o.setOrderCount(DataUtil.Integerr.GetValueOrDefault(m
						.get("OrderCount")));
			}
		}
		return l;
	}

	// 查询忽略风控 2016-08-21 http://jira.ymatou.cn:8080/browse/JIRACONN-823
	@Deprecated
	private List<Integer> setSellerOrderStatusControlParams() {
		List<Integer> result = null;
		List<Integer> OrderStatusList = getsellerordercountbean
				.getOrderStatusList();
		if (null != OrderStatusList) {
			if (SellerOrderStatusParams.RiskControllerDisabled) {
				sellerOrderStatusParams.setConsiderOrderStatus(true);
				sellerOrderStatusParams.setConsiderRestOrderStatus(true);
				return OrderStatusList;
			}
			sellerOrderStatusParams.setConsiderRiskVerfiedStatus(true);
			List<Integer> OrderStatusList2 = OrderStatusList;
			sellerOrderStatusParams.setConsiderOrderStatus(true);

			if (OrderStatusList2.contains(OrderStatusEnum.OrderEstablish
					.getValue())) {
				sellerOrderStatusParams.setConsiderRCOrderEstablish(true);
				OrderStatusList2
						.removeIf(a -> a == OrderStatusEnum.OrderEstablish
								.getValue());
			}
			if (OrderStatusList2.contains(OrderStatusEnum.AccountPaid
					.getValue())) {
				sellerOrderStatusParams.setConsiderRCAccountPaid(true);
				OrderStatusList2.removeIf(a -> a == OrderStatusEnum.AccountPaid
						.getValue());
			}
			if (OrderStatusList2.size() > 0) {
				sellerOrderStatusParams.setConsiderRestOrderStatus(true);
			}
			result = OrderStatusList2;
		}
		return result;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_001() {
		Logger.start(true, "获取商家非闪购订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-200);
			getsellerordercountbean.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1);
			getsellerordercountbean.setEndTime(EndTime);
			// 是否为闪购订单
			getsellerordercountbean.setIsShangouOrder(false);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(4);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_002() {
		Logger.start(true, "获取商家闪购订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-200);
			getsellerordercountbean.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1);
			getsellerordercountbean.setEndTime(EndTime);
			// 是否为闪购订单
			getsellerordercountbean.setIsShangouOrder(true);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(4);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			// 是否为闪购订单
			getsellerordercountbean.setIsShangouOrder(false);
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall2 send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall2 response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_003() {
		Logger.start(true, "获取商家闪购订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40);
			getsellerordercountbean.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1);
			getsellerordercountbean.setEndTime(EndTime);
			// 是否为闪购订单
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(4);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			getsellerordercountbean.setIsShangouOrder(true);
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			// 是否为闪购订单
			getsellerordercountbean.setIsShangouOrder(false);
			Logger.debug("getsellerordercountbean:"
					+ getsellerordercountbean.getIsShangouOrder());
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_004() {
		Logger.start(true,
				"获取商家订单数量成功, 只传sellerid和OrderStatusList,OrderStatusList=[2,101,17]");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(2);
			OrderStatusList.add(101);
			OrderStatusList.add(17);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_005() {
		Logger.start(
				true,
				"获取商家订单数量成功, 只传sellerid和OrderStatusList,OrderStatusList=[1,2,101,16,17,3,4,18,12,13]");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(1);
			OrderStatusList.add(2);
			OrderStatusList.add(101);
			OrderStatusList.add(16);
			OrderStatusList.add(17);
			OrderStatusList.add(3);
			OrderStatusList.add(4);
			OrderStatusList.add(18);
			OrderStatusList.add(12);
			OrderStatusList.add(13);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_006() {
		Logger.start(
				true,
				"获取商家订单数量成功, 只传sellerid和OrderStatusList,BeginTime和EndTime,OrderStatusList=[1,2,101,16,17,3,4],BeginTime和EndTime间隔一天");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(1);
			OrderStatusList.add(2);
			OrderStatusList.add(101);
			OrderStatusList.add(16);
			OrderStatusList.add(17);
			OrderStatusList.add(3);
			OrderStatusList.add(4);
			getsellerordercountbean.setOrderStatusList(OrderStatusList);
			getsellerordercountbean.setSellerId(SellerId);
			getsellerordercountbean.setBeginTime(YMTDateUtil
					.getBeforeOrNextDay(-1));
			getsellerordercountbean.setEndTime(YMTDateUtil
					.getBeforeOrNextDay(0));
			// 发送请求
			getsellerordercountcall.setData(getsellerordercountbean);
			getsellerordercountcall.callService();
			Logger.comment("getsellerordercountcall send:"
					+ getsellerordercountcall.getEntityString());
			Logger.comment("getsellerordercountcall response:"
					+ getsellerordercountcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordercountcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}